﻿Ext.BLANK_IMAGE_URL = '../ext2/resources/images/default/s.gif';
Ext.lib.Ajax.defaultPostHeader += '; charset=UTF-8';
Ext.QuickTips.init();


 		var balance_tempReader =  new Ext.data.JsonReader({
			     totalProperty: "results", //results里面的数据是数据的条数
			     root:"items",
			     fields : [
			     	 {name: 'id'},
			         {name: 'adminId'},
			         {name: 'adminName'},
			         {name: 'money'},
			         {name: 'state'},
			         {name: 'sagmtTime'}
			         ]   
		});

 		var balance_groupingStore = new Ext.data.GroupingStore({	   
			   groupOnSort:false, autoLoad :false,
			   reader: balance_tempReader,
			   url : '../admin/balance/action/Com_getBalanceList.do'	
        }); 
        _params={start : 0,limit :limit};
		balance_groupingStore.on("beforeload", function(thiz, options) {
				 	       thiz.baseParams=_params;
		        	});
        
          var balance_toolbar =  new Ext.PagingToolbar({         	
	        store : balance_groupingStore,
			pageSize : limit,
			displayInfo : true,
			displayMsg : '第 {0} 条到 {1} 条，一共 {2} 条',
			emptyMsg : "没有记录"	,
	      	items:[
				  { enableToggle: true,
                    text: '己支付',    
                    cls: 'x-btn-text-icon details',icon:"../ext2/images/edit.png",
                    toggleHandler: balance_ystate
                   },{ enableToggle: true,
                    text: '未支付',    
                    cls: 'x-btn-text-icon details',icon:"../ext2/images/edit.png",
                    toggleHandler: balance_wstate
                   },{ enableToggle: true,
                    text: '删除',    
                    cls: 'x-btn-text-icon details',icon:"../ext2/images/delete.gif",
                    toggleHandler: balance_all
                   }
				]
		});
		
		var balance_cb = new Ext.grid.CheckboxSelectionModel();
		
		//列模型

		var balance_colModel = new Ext.grid.ColumnModel( [                                             //配置表格列

			         new Ext.grid.RowNumberer({
					       header : '行号',                  
					       width : 45
				     }), 	
				 balance_cb,
				{header: "ID", width: 40, dataIndex: 'id', sortable: true},
				{header: "会员ID", width: 180, dataIndex: 'adminId', sortable: true},
				{header: "会员名称", width: 120, dataIndex: 'adminName', sortable: true},
				{header: "结算时间", width: 280, dataIndex: 'sagmtTime', sortable: true},
				{header: "结算钱数", width: 120, dataIndex: 'money', sortable: true,renderer:balanceCss},
				{header: "结算状态", width: 120, dataIndex: 'state', sortable: true},
				{header: "操作", width: 80, dataIndex: 'id', renderer:balanceRend}
			]); 
			
			
		var balance_grid = Ext.extend(Ext.grid.GridPanel, {
			id : 'balance_grid',
			title : '结算数据管理',
			closable : true,
			frame : true,
			width : document.body.clientWidth - 190,
			height : document.body.clientHeight - 137,
			stripeRows : true,
			loadMask : true,                                      // 载入数据时遮盖住页面
			sm : balance_cb,                                       // 这里设置后，就可以实现全选功能
			tbar : balance_toolbar,
			store : balance_groupingStore,
			// 设置表格分组视图
			view : new Ext.grid.GroupingView({
				groupTextTpl : '{text}(共{[values.rs.length]}条)', // 定义分组行模板

				groupByText : '使用当前字段进行分组',                 // 表头菜单提示信息
				showGroupsText : '表格分组',                        // 表头菜单提示信息
				showGroupName : true,                             // 显示分组字段名称
				startCollapsed : false,                           // 展开分组
				hideGroupedColumn : false
				}),

			cm : balance_colModel

		});

		
		//相关操作
		function balanceRend(value){		    
		    var strurl = "<a href='javascript:balance_delete(\""+value+"\")'><img style='margin-left: 6px;'  src='../ext2/images/delAll.gif' title='删除结算数据' /></a>";		   
		    return strurl;
		}
		
		function balanceCss(value){
			return value+"<font color='red'>￥</font>"
		}
		
		
		//己支付状态
		function balance_ystate(){
			if(_adminId =="1"){
				var ids = getInfoIds("balance_grid");
			    Ext.MessageBox.confirm("提示","确定要将这 "+ids.length+" 条数据全部修改为己支付状态吗？",function(btnId)
				{
					if(btnId == 'yes')
					{
						balance_ajax(ids,"己支付",'../admin/balance/action/Com_balanceUpState.do');
					}
				});	
			}
		}
		
		//己支付状态
		function balance_wstate(){
			if(_adminId =="1"){
				var ids = getInfoIds("balance_grid");
				Ext.MessageBox.confirm("提示","确定要将这 "+ids.length+" 条数据全部修改为未支付状态吗？",function(btnId)
				{
					if(btnId == 'yes')
					{
						balance_ajax(ids,"未支付",'../admin/balance/action/Com_balanceUpState.do');
					}
				});	
			}
		}
		
		//删除结算数据
		function balance_all(){
			if(_adminId =="1"){
				var ids = getInfoIds("balance_grid");
			    Ext.MessageBox.confirm("提示","确定要将这 "+ids.length+" 条数据全部删除吗？",function(btnId)
				{
					if(btnId == 'yes')
					{
						balance_ajax(ids,null,'../admin/balance/action/Com_balanceDelete.do');
					}
				});	
			}
		}
		
		//获取选中数据的id
		function getInfoIds(grid_name){
			var recs;
			recs = Ext.getCmp(grid_name).getSelectionModel().getSelections();
			var list = [];
			if(recs.length == 0){
				Ext.Msg.alert('提示','请选择要进行操作的数据！');
				return;
			}else{
				for(var i = 0 ; i < recs.length ; i++){
					var rec = recs[i];
					list.push(rec.get('id'));
				}
			}
			return list;
		}
		
		//单条数据删除
		function balance_delete(_id){
			if(_adminId =="1"){
				var msgTip = Ext.MessageBox.show({
				title:'Tips',
				width : 250,
				msg:'后台正在执行请求,请稍后......'
			});
			Ext.Ajax.request({
				url : '../admin/balance/action/Com_balanceDelete.do',
				params : {ids : _id},
				method : 'POST',
				success : function(response,options){
					msgTip.hide();
					var result = Ext.util.JSON.decode(response.responseText);
					if(result.success){
						//服务器端数据成功删除后，同步删除客户端列表中的数据
						balance_groupingStore.load();
					}else{
						Ext.Msg.alert('提示',result.msg);
					}
				},
				failure : function(response,options){
					msgTip.hide();
					Ext.Msg.alert('提示','数据操作失败!');
				}
			});		
			}
		}
		
		//状态修改
		function balance_ajax(_id,_state,_url){
			if(_adminId =="1"){
				var msgTip = Ext.MessageBox.show({
				title:'Tips',
				width : 250,
				msg:'后台正在执行请求,请稍后......'
			});
			Ext.Ajax.request({
				url : _url,
				params : {ids : _id,state:_state},
				method : 'POST',
				success : function(response,options){
					msgTip.hide();
					var result = Ext.util.JSON.decode(response.responseText);
					if(result.success){
						//服务器端数据成功删除后，同步删除客户端列表中的数据
						balance_groupingStore.load();
					}else{
						Ext.Msg.alert('提示',result.msg);
					}
				},
				failure : function(response,options){
					msgTip.hide();
					Ext.Msg.alert('提示','数据操作失败!');
				}
			});		
			}
		}

Ext.Window.prototype.beforeDestroy = function() {
	Ext.destroy(this.focusEl,   // 新增
			this.bwrap,         // 新增     
			this.resizer, this.dd, this.proxy, this.mask);
	Ext.Window.superclass.beforeDestroy.call(this);

	this.focusEl = null;        // 新增   
	this.bwrap = null;          // 新增   
}
